// 本文件对Data的相关功能进行性能测试
// @Author: zhengqi Wu
// @Description:
// @Version: 1.0.0
// @Date: 2021/4/16 13:11 下午
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
//

const benchmark = require('benchmark')
const {createPayLoad} = require("../common/createPayload");
const {createBlockByBuffer} = require("../encoding/Block");
const {createIdentifierByString} = require("../component/Identifier");
const {Data} = require("./Data");
const {Encoder, MaxPacketSize} = require('../encoding').Encoder
const payload = createPayLoad(0), payload1K = createPayLoad(1), payload8K = createPayLoad(8)

function createData(payload) {
    let data = new Data()
    data.FreshnessPeriod.setFreshnessPeriod(1)
    data.Payload.setValue(new Uint8Array(payload))
    data.CongestionMark.setCongestionLevel(1)
    let name = createIdentifierByString('/qjm')
    data.setName(name)
    return data
}
let data = createData(payload), data1K = createData(payload1K), data8K = createData(payload8K)
// 测试WireEncode
const suite2 = new benchmark.Suite()
suite2.add('wireEncode 1Byte', ()=>{
    let enc = new Encoder()
    enc.encoderReset(MaxPacketSize, 0)
    data.wireEncode(enc)
}).add('wireEncode 1K', ()=>{
    let enc = new Encoder()
    enc.encoderReset(MaxPacketSize, 0)
    data1K.wireEncode(enc)
}).add('wireEncode 8K', ()=>{
    let enc = new Encoder()
    enc.encoderReset(MaxPacketSize, 0)
    data8K.wireEncode(enc)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('wireEncode finished')
}).run()

// 测试WireDecode
let enc = new Encoder()
enc.encoderReset(MaxPacketSize, 0)
data.wireEncode(enc)
let buf = enc.getBuffer()
let data2 = new Data()
let block = createBlockByBuffer(buf, false)
// let enc = new Encoder()
enc.encoderReset(MaxPacketSize, 0)
data1K.wireEncode(enc)
buf = enc.getBuffer()
let data2_1K = new Data()
let block1K = createBlockByBuffer(buf, false)
// let enc = new Encoder()
enc.encoderReset(MaxPacketSize, 0)
data8K.wireEncode(enc)
buf = enc.getBuffer()
let data2_8K = new Data()
let block8K = createBlockByBuffer(buf, false)
const suite3 = new benchmark.Suite()
suite3.add('wireDecode 1Byte', ()=>{
    data2.wireDecode(block)
}).add('wireDecode 1K', ()=>{
    data2_1K.wireDecode(block1K)
}).add('wireDecode 8K', ()=>{
    data2_8K.wireDecode(block8K)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('wireDecode finished')
}).run()
